package com.zhou.scheduled;


import com.zhou.configure.PushConfigure;
import com.zhou.dto.HotSpot;
import com.zhou.dto.IdentityInfo;
import com.zhou.dto.Result;
import com.zhou.repository.IdentityInfoDataRepository;
import com.zhou.service.OtherInterfaceService;
import com.zhou.service.PushService;
import com.zhou.service.WeatherService;
import com.zhou.utils.MemoryDayUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lcz
 * @desc 定时任务
 * @mail lcz_0130@163.com
 * @date 2023/12/27
 */

@EnableScheduling
@Configuration
public class TimedTasks {
    @Resource
    private PushService pushService;
    @Resource
    private PushConfigure pushConfigure;
    @Resource
    private WeatherService weatherService;
    @Resource
    private OtherInterfaceService otherInterfaceService;
    @Resource
    private IdentityInfoDataRepository identityInfoDataRepository;

    /**
     * 每天早上8点给用户推送模板消息
     */
//    @Scheduled(cron = "0 0 8 * * ?")
    public void pushTemplate() {
        //判断是否开启模板消息推送
        if (pushConfigure.isTemplate_switch()) {
            for (IdentityInfo identityInfo : identityInfoDataRepository.findAll()) {
                pushService.pushTemplate(identityInfo);
            }
        }
    }

    /**
     * 每天早上8点给用户推送自定义消息
     *
     * @return
     */
    @Scheduled(cron = "0 0 8 * * ?")
    public void pushCustom() {
        // 计算天数
        long loveDays = MemoryDayUtil.calculationLianAi(pushConfigure.getLoveDate());
        long birthdays = 0;
        if (pushConfigure.isUseLunar()) {
            // 如果使用农历生日
            birthdays = MemoryDayUtil.calculationBirthdayByLunar(pushConfigure.getBirthday());
        } else {
            birthdays = MemoryDayUtil.calculationBirthday(pushConfigure.getBirthday());
        }
        String remark = "";
        if (loveDays % 365 == 0) {
            remark = "\n今天是恋爱" + (loveDays / 365) + "周年纪念日!";
        }
        if (birthdays == 0) {
            remark = "\n今天是生日,生日快乐呀!";
        }
        if (loveDays % 365 == 0 && birthdays == 0) {
            remark = "\n今天是生日,也是恋爱" + (loveDays / 365) + "周年纪念日!";
        }
        for (IdentityInfo identityInfo : identityInfoDataRepository.findAll()) {
            Map<String, String> personalInformationMap = otherInterfaceService.getWeatherMap(identityInfo);
            String content = "早上好~" + "\n\n" + "当前位置：" + personalInformationMap.get("location") + "\n" +
                    "\n" +
                    "实时天气：" + personalInformationMap.get("now_temp") + "°C  " + personalInformationMap.get("now_weather") + "\n" +
                    "气象：" + personalInformationMap.get("now_wind_dir") + "" + personalInformationMap.get("now_wind_class") + " | 湿度" + personalInformationMap.get("now_rh") + "%\n" +
                    "今天：" + personalInformationMap.get("today_weather") + "  " + personalInformationMap.get("today_high") + "/" + personalInformationMap.get("today_low") + "°C\n" +
                    "\n" + "今天是我们恋爱的第" + loveDays + "天\n" +
                    "距离宝宝的生日还有" + birthdays + "天\n" +
                    remark + "\n" +
                    "\n" + personalInformationMap.get("poem");
            pushService.pushCustom(content, identityInfo.getOpenId());
        }
    }

    /**
     * 每天晚上10点30分给用户推送自定义消息
     */
    @Scheduled(cron = "0 30 22 * * ?")
    public void pushCustom2() {
        for (IdentityInfo identityInfo : identityInfoDataRepository.findAll()) {
            Map<String, String> personalInformationMap = otherInterfaceService.getWeatherMap(identityInfo);
            String content = "当前位置：" + personalInformationMap.get("location") + "\n" +
                    "\n" +
                    "晚安啦~❤❤\n" +
                    "\n" +
                    "今天一天辛苦啦~\n" +
                    "晚上睡觉记得盖好被子哦❤\n" +
                    "\n" + personalInformationMap.get("poem");
            pushService.pushCustom(content, identityInfo.getOpenId());
        }
    }

    /**
     * 每天晚上17点推送热点
     */
//    @Scheduled(cron = "0 0 17 * * ?")
    public void pushNews() {
        Result hotSpot = otherInterfaceService.getHotSpot(pushConfigure.getColaKey());
        List<HotSpot> hotSpotList;
        if (!"200".equals(hotSpot.getCode())) {
            return;
        }
        hotSpotList = (List<HotSpot>) hotSpot.getData();

        for (IdentityInfo identityInfo : identityInfoDataRepository.findAll()) {
            hotSpotList.forEach(hotSpot1 -> {
                Map<String, String> requestMap = new HashMap<>();
                requestMap.put("title", hotSpot1.getSitename());
                requestMap.put("description", hotSpot1.getTitle());
                requestMap.put("picUrl", hotSpot1.getLogo());
                requestMap.put("url", hotSpot1.getUrl());
                requestMap.put("openId", identityInfo.getOpenId());
                pushService.pushNews(requestMap);
            });
        }
    }


}
